package longrise.phone.com.baidulib.baidu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.ly.common.commonutils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.baidulib.R;

/**
 * Created by luoyang on 2017-8-8.
 */

public class MapNavUtils implements SelectMapDialog.OnSelectPhotoListener {

    private Activity mContext;
    private double mLatitude;
    private double mLongitude;
    private String mTerminal;

    private SelectMapDialog mDialog;

    private boolean hasBaiduMap;

    private boolean hasGaodeMap;

    /**
     *
     * @param context   当前跳转界面
     */
    public MapNavUtils(Activity context) {
        this.mContext = context;
        init();
    }

    private void init() {
        hasBaiduMap = isAvilible(mContext, "com.baidu.BaiduMap");
        hasGaodeMap = isAvilible(mContext,  "com.autonavi.minimap");
        initSelectMapDialog();
    }

    public void start(double latitude, double longitude, String terminal) {
        this.mLatitude = latitude;
        this.mLongitude = longitude;
        this.mTerminal = terminal;

        if(!hasBaiduMap && !hasGaodeMap) {
            //给提示
            Toast.makeText(mContext, "您的手机未安装百度或高德地图，无法为您导航", Toast.LENGTH_LONG).show();
        } else if(hasBaiduMap && !hasGaodeMap) {
            //只安装了百度地图
            navToBaiduMap();
        } else if(!hasBaiduMap && hasGaodeMap) {
            //只安装了高德地图
            navToGaodeMap();
        } else if(hasBaiduMap && hasGaodeMap) {
            //百度与高德都安装
            if(mDialog != null) {
                mDialog.show();
            }
        }
    }

    private void navToBaiduMap() {
        try {
            String location = mLatitude + "," + mLongitude;
            Intent nav = new Intent();
            if(mTerminal!=null && !StringUtils.isEmpty(mTerminal)){
            nav.setData(Uri.parse("baidumap://map/direction?destination="+ mTerminal));
            }else{
            nav.setData(Uri.parse("baidumap://map/navi?location=" + location));
            }
            mContext.startActivity(nav);
        } catch (Exception e) {
            Log.e("intent", e.getMessage());
        }
    }

    private void navToGaodeMap() {
        try {
            if(mTerminal!=null && !StringUtils.isEmpty(mTerminal)){
                Intent intent = Intent.getIntent("androidamap://route?sourceApplication=softname&dname="+mTerminal+"&dev=0&m=0&t=1");
                mContext.startActivity(intent);
            }else{
                Intent gaode = new Intent("android.intent.action.VIEW",
                        Uri.parse("androidamap://navi?sourceApplication=汽车档案&lat=" +
                                mLatitude + "&lon=" + mLongitude + "&dev=0"));
                gaode.setPackage("com.autonavi.minimap");
                mContext.startActivity(gaode);
            }
        } catch (Exception e) {
            Log.e("intent", e.getMessage());
        }
    }

    private void initSelectMapDialog() {
        mDialog = new SelectMapDialog(mContext, R.style.dialog_style);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setOnSelectPhotoListener(this);
//        mDialog.show();
    }

    /**
     *检测手机上是否装了指定包名
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isAvilible(Context context, String packageName){
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if(packageInfos != null){
            for(int i = 0; i < packageInfos.size(); i++){
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

    @Override
    public void onSelect(int type) {
        switch (type) {
            case SelectMapDialog.SELECT_BAIDU :
                navToBaiduMap();
                break;
            case SelectMapDialog.SELECT_GAODE :
                navToGaodeMap();
                break;
            case SelectMapDialog.SELECT_CANCEL :
                break;
        }
        mDialog.dismiss();
    }



}
